這篇的上一篇:https://ithelp.ithome.com.tw/articles/10283960
這裡要來寫....還是JSON
開新檔-發現模擬器要在512MB下開才跑得比較快
然後這裡是抓下來的JSON資料:
{\"SiteName\":\"二林\",\"County\":\"彰化縣\",\"AQI\":\"37\",\"Pollutant\":\"\",\"Status\":\"良好\",\"SO2\":\"1\",\"CO\":\"0.25\",\"CO_8hr\":\"0.3\",\"O3\":\"27.6\",\"O3_8hr\":\"29\",\"PM10\":\"24\",\"PM2.5\":\"8\",\"NO2\":\"5\",\"NOx\":\"6.5\",\"NO\":\"1.6\",\"WindSpeed\":\"6.4\",\"WindDirec\":\"25\",\"PublishTime\":\"2020/12/18 18:00:00\",\"PM2.5_AVG\":\"12\",\"PM10_AVG\":\"30\",\"SO2_AVG\":\"2\",\"Longitude\":\"120.409653\",\"Latitude\":\"23.925175\",\"SiteId\":\"35\"},\n" +
"{\"SiteName\":\"三重\",\"County\":\"新北市\",\"AQI\":\"59\",\"Pollutant\":\"細懸浮微粒\",\"Status\":\"普通\",\"SO2\":\"4.9\",\"CO\":\"3.13\",\"CO_8hr\":\"2.2\",\"O3\":\"-\",\"O3_8hr\":\"-\",\"PM10\":\"23\",\"PM2.5\":\"16\",\"NO2\":\"49\",\"NOx\":\"208.4\",\"NO\":\"159.4\",\"WindSpeed\":\"-\",\"WindDirec\":\"-\",\"PublishTime\":\"2020/12/18 18:00:00\",\"PM2.5_AVG\":\"19\",\"PM10_AVG\":\"29\",\"SO2_AVG\":\"3\",\"Longitude\":\"121.493806\",\"Latitude\":\"25.072611\",\"SiteId\":\"67\"},\n" +
"{\"SiteName\":\"三義\",\"County\":\"苗栗縣\",\"AQI\":\"26\",\"Pollutant\":\"\",\"Status\":\"良好\",\"SO2\":\"1.9\",\"CO\":\"0.25\",\"CO_8hr\":\"0.2\",\"O3\":\"21.3\",\"O3_8hr\":\"26\",\"PM10\":\"11\",\"PM2.5\":\"3\",\"NO2\":\"11.6\",\"NOx\":\"12.7\",\"NO\":\"1.2\",\"WindSpeed\":\"6\",\"WindDirec\":\"20\",\"PublishTime\":\"2020/12/18 18:00:00\",\"PM2.5_AVG\":\"8\",\"PM10_AVG\":\"12\",\"SO2_AVG\":\"2\",\"Longitude\":\"120.758833\",\"Latitude\":\"24.382942\",\"SiteId\":\"27\"}
放入JAVA 檔-
package com.huang.jsonagain1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
String json="[]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
再加入JSON資料
package com.huang.jsonagain1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
String json="[{\\\"SiteName\\\":\\\"二林\\\",\\\"County\\\":\\\"彰化縣\\\",\\\"AQI\\\":\\\"37\\\",\\\"Pollutant\\\":\\\"\\\",\\\"Status\\\":\\\"良好\\\",\\\"SO2\\\":\\\"1\\\",\\\"CO\\\":\\\"0.25\\\",\\\"CO_8hr\\\":\\\"0.3\\\",\\\"O3\\\":\\\"27.6\\\",\\\"O3_8hr\\\":\\\"29\\\",\\\"PM10\\\":\\\"24\\\",\\\"PM2.5\\\":\\\"8\\\",\\\"NO2\\\":\\\"5\\\",\\\"NOx\\\":\\\"6.5\\\",\\\"NO\\\":\\\"1.6\\\",\\\"WindSpeed\\\":\\\"6.4\\\",\\\"WindDirec\\\":\\\"25\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"12\\\",\\\"PM10_AVG\\\":\\\"30\\\",\\\"SO2_AVG\\\":\\\"2\\\",\\\"Longitude\\\":\\\"120.409653\\\",\\\"Latitude\\\":\\\"23.925175\\\",\\\"SiteId\\\":\\\"35\\\"},\\n\" +\n" +
" \"{\\\"SiteName\\\":\\\"三重\\\",\\\"County\\\":\\\"新北市\\\",\\\"AQI\\\":\\\"59\\\",\\\"Pollutant\\\":\\\"細懸浮微粒\\\",\\\"Status\\\":\\\"普通\\\",\\\"SO2\\\":\\\"4.9\\\",\\\"CO\\\":\\\"3.13\\\",\\\"CO_8hr\\\":\\\"2.2\\\",\\\"O3\\\":\\\"-\\\",\\\"O3_8hr\\\":\\\"-\\\",\\\"PM10\\\":\\\"23\\\",\\\"PM2.5\\\":\\\"16\\\",\\\"NO2\\\":\\\"49\\\",\\\"NOx\\\":\\\"208.4\\\",\\\"NO\\\":\\\"159.4\\\",\\\"WindSpeed\\\":\\\"-\\\",\\\"WindDirec\\\":\\\"-\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"19\\\",\\\"PM10_AVG\\\":\\\"29\\\",\\\"SO2_AVG\\\":\\\"3\\\",\\\"Longitude\\\":\\\"121.493806\\\",\\\"Latitude\\\":\\\"25.072611\\\",\\\"SiteId\\\":\\\"67\\\"},\\n\" +\n" +
" \"{\\\"SiteName\\\":\\\"三義\\\",\\\"County\\\":\\\"苗栗縣\\\",\\\"AQI\\\":\\\"26\\\",\\\"Pollutant\\\":\\\"\\\",\\\"Status\\\":\\\"良好\\\",\\\"SO2\\\":\\\"1.9\\\",\\\"CO\\\":\\\"0.25\\\",\\\"CO_8hr\\\":\\\"0.2\\\",\\\"O3\\\":\\\"21.3\\\",\\\"O3_8hr\\\":\\\"26\\\",\\\"PM10\\\":\\\"11\\\",\\\"PM2.5\\\":\\\"3\\\",\\\"NO2\\\":\\\"11.6\\\",\\\"NOx\\\":\\\"12.7\\\",\\\"NO\\\":\\\"1.2\\\",\\\"WindSpeed\\\":\\\"6\\\",\\\"WindDirec\\\":\\\"20\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"8\\\",\\\"PM10_AVG\\\":\\\"12\\\",\\\"SO2_AVG\\\":\\\"2\\\",\\\"Longitude\\\":\\\"120.758833\\\",\\\"Latitude\\\":\\\"24.382942\\\",\\\"SiteId\\\":\\\"27\\\"}\n" +
" ]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
要用JSONArray 然後再修正錯誤-->try catch
package com.huang.jsonagain1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import org.json.JSONArray;
public class MainActivity extends AppCompatActivity {
String json="[{\\\"SiteName\\\":\\\"二林\\\",\\\"County\\\":\\\"彰化縣\\\",\\\"AQI\\\":\\\"37\\\",\\\"Pollutant\\\":\\\"\\\",\\\"Status\\\":\\\"良好\\\",\\\"SO2\\\":\\\"1\\\",\\\"CO\\\":\\\"0.25\\\",\\\"CO_8hr\\\":\\\"0.3\\\",\\\"O3\\\":\\\"27.6\\\",\\\"O3_8hr\\\":\\\"29\\\",\\\"PM10\\\":\\\"24\\\",\\\"PM2.5\\\":\\\"8\\\",\\\"NO2\\\":\\\"5\\\",\\\"NOx\\\":\\\"6.5\\\",\\\"NO\\\":\\\"1.6\\\",\\\"WindSpeed\\\":\\\"6.4\\\",\\\"WindDirec\\\":\\\"25\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"12\\\",\\\"PM10_AVG\\\":\\\"30\\\",\\\"SO2_AVG\\\":\\\"2\\\",\\\"Longitude\\\":\\\"120.409653\\\",\\\"Latitude\\\":\\\"23.925175\\\",\\\"SiteId\\\":\\\"35\\\"},\\n\" +\n" +
" \"{\\\"SiteName\\\":\\\"三重\\\",\\\"County\\\":\\\"新北市\\\",\\\"AQI\\\":\\\"59\\\",\\\"Pollutant\\\":\\\"細懸浮微粒\\\",\\\"Status\\\":\\\"普通\\\",\\\"SO2\\\":\\\"4.9\\\",\\\"CO\\\":\\\"3.13\\\",\\\"CO_8hr\\\":\\\"2.2\\\",\\\"O3\\\":\\\"-\\\",\\\"O3_8hr\\\":\\\"-\\\",\\\"PM10\\\":\\\"23\\\",\\\"PM2.5\\\":\\\"16\\\",\\\"NO2\\\":\\\"49\\\",\\\"NOx\\\":\\\"208.4\\\",\\\"NO\\\":\\\"159.4\\\",\\\"WindSpeed\\\":\\\"-\\\",\\\"WindDirec\\\":\\\"-\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"19\\\",\\\"PM10_AVG\\\":\\\"29\\\",\\\"SO2_AVG\\\":\\\"3\\\",\\\"Longitude\\\":\\\"121.493806\\\",\\\"Latitude\\\":\\\"25.072611\\\",\\\"SiteId\\\":\\\"67\\\"},\\n\" +\n" +
" \"{\\\"SiteName\\\":\\\"三義\\\",\\\"County\\\":\\\"苗栗縣\\\",\\\"AQI\\\":\\\"26\\\",\\\"Pollutant\\\":\\\"\\\",\\\"Status\\\":\\\"良好\\\",\\\"SO2\\\":\\\"1.9\\\",\\\"CO\\\":\\\"0.25\\\",\\\"CO_8hr\\\":\\\"0.2\\\",\\\"O3\\\":\\\"21.3\\\",\\\"O3_8hr\\\":\\\"26\\\",\\\"PM10\\\":\\\"11\\\",\\\"PM2.5\\\":\\\"3\\\",\\\"NO2\\\":\\\"11.6\\\",\\\"NOx\\\":\\\"12.7\\\",\\\"NO\\\":\\\"1.2\\\",\\\"WindSpeed\\\":\\\"6\\\",\\\"WindDirec\\\":\\\"20\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"8\\\",\\\"PM10_AVG\\\":\\\"12\\\",\\\"SO2_AVG\\\":\\\"2\\\",\\\"Longitude\\\":\\\"120.758833\\\",\\\"Latitude\\\":\\\"24.382942\\\",\\\"SiteId\\\":\\\"27\\\"}\n" +
" ]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JSONArray jsonArray = new JSONArray(json);
}
}
然後再來修正裡面的內容變成: Log.e("Howard","JSONException"+e);
package com.huang.jsonagain1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
public class MainActivity extends AppCompatActivity {
String json="[{\\\"SiteName\\\":\\\"二林\\\",\\\"County\\\":\\\"彰化縣\\\",\\\"AQI\\\":\\\"37\\\",\\\"Pollutant\\\":\\\"\\\",\\\"Status\\\":\\\"良好\\\",\\\"SO2\\\":\\\"1\\\",\\\"CO\\\":\\\"0.25\\\",\\\"CO_8hr\\\":\\\"0.3\\\",\\\"O3\\\":\\\"27.6\\\",\\\"O3_8hr\\\":\\\"29\\\",\\\"PM10\\\":\\\"24\\\",\\\"PM2.5\\\":\\\"8\\\",\\\"NO2\\\":\\\"5\\\",\\\"NOx\\\":\\\"6.5\\\",\\\"NO\\\":\\\"1.6\\\",\\\"WindSpeed\\\":\\\"6.4\\\",\\\"WindDirec\\\":\\\"25\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"12\\\",\\\"PM10_AVG\\\":\\\"30\\\",\\\"SO2_AVG\\\":\\\"2\\\",\\\"Longitude\\\":\\\"120.409653\\\",\\\"Latitude\\\":\\\"23.925175\\\",\\\"SiteId\\\":\\\"35\\\"},\\n\" +\n" +
" \"{\\\"SiteName\\\":\\\"三重\\\",\\\"County\\\":\\\"新北市\\\",\\\"AQI\\\":\\\"59\\\",\\\"Pollutant\\\":\\\"細懸浮微粒\\\",\\\"Status\\\":\\\"普通\\\",\\\"SO2\\\":\\\"4.9\\\",\\\"CO\\\":\\\"3.13\\\",\\\"CO_8hr\\\":\\\"2.2\\\",\\\"O3\\\":\\\"-\\\",\\\"O3_8hr\\\":\\\"-\\\",\\\"PM10\\\":\\\"23\\\",\\\"PM2.5\\\":\\\"16\\\",\\\"NO2\\\":\\\"49\\\",\\\"NOx\\\":\\\"208.4\\\",\\\"NO\\\":\\\"159.4\\\",\\\"WindSpeed\\\":\\\"-\\\",\\\"WindDirec\\\":\\\"-\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"19\\\",\\\"PM10_AVG\\\":\\\"29\\\",\\\"SO2_AVG\\\":\\\"3\\\",\\\"Longitude\\\":\\\"121.493806\\\",\\\"Latitude\\\":\\\"25.072611\\\",\\\"SiteId\\\":\\\"67\\\"},\\n\" +\n" +
" \"{\\\"SiteName\\\":\\\"三義\\\",\\\"County\\\":\\\"苗栗縣\\\",\\\"AQI\\\":\\\"26\\\",\\\"Pollutant\\\":\\\"\\\",\\\"Status\\\":\\\"良好\\\",\\\"SO2\\\":\\\"1.9\\\",\\\"CO\\\":\\\"0.25\\\",\\\"CO_8hr\\\":\\\"0.2\\\",\\\"O3\\\":\\\"21.3\\\",\\\"O3_8hr\\\":\\\"26\\\",\\\"PM10\\\":\\\"11\\\",\\\"PM2.5\\\":\\\"3\\\",\\\"NO2\\\":\\\"11.6\\\",\\\"NOx\\\":\\\"12.7\\\",\\\"NO\\\":\\\"1.2\\\",\\\"WindSpeed\\\":\\\"6\\\",\\\"WindDirec\\\":\\\"20\\\",\\\"PublishTime\\\":\\\"2020/12/18 18:00:00\\\",\\\"PM2.5_AVG\\\":\\\"8\\\",\\\"PM10_AVG\\\":\\\"12\\\",\\\"SO2_AVG\\\":\\\"2\\\",\\\"Longitude\\\":\\\"120.758833\\\",\\\"Latitude\\\":\\\"24.382942\\\",\\\"SiteId\\\":\\\"27\\\"}\n" +
" ]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
JSONArray jsonArray = new JSONArray(json);
} catch (JSONException e) {
Log.e("Howard","JSONException"+e);
}
}
}
然後因為要讀裡面的資料要用FOR迴圈-->然後取出-->最後輸出
package com.huang.jsonagain1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
String json = "[{\"SiteName\":\"二林\",\"County\":\"彰化縣\",\"AQI\":\"37\",\"Pollutant\":\"\",\"Status\":\"良好\",\"SO2\":\"1\",\"CO\":\"0.25\",\"CO_8hr\":\"0.3\",\"O3\":\"27.6\",\"O3_8hr\":\"29\",\"PM10\":\"24\",\"PM2.5\":\"8\",\"NO2\":\"5\",\"NOx\":\"6.5\",\"NO\":\"1.6\",\"WindSpeed\":\"6.4\",\"WindDirec\":\"25\",\"PublishTime\":\"2020/12/18 18:00:00\",\"PM2.5_AVG\":\"12\",\"PM10_AVG\":\"30\",\"SO2_AVG\":\"2\",\"Longitude\":\"120.409653\",\"Latitude\":\"23.925175\",\"SiteId\":\"35\"},\n" +
"{\"SiteName\":\"三重\",\"County\":\"新北市\",\"AQI\":\"59\",\"Pollutant\":\"細懸浮微粒\",\"Status\":\"普通\",\"SO2\":\"4.9\",\"CO\":\"3.13\",\"CO_8hr\":\"2.2\",\"O3\":\"-\",\"O3_8hr\":\"-\",\"PM10\":\"23\",\"PM2.5\":\"16\",\"NO2\":\"49\",\"NOx\":\"208.4\",\"NO\":\"159.4\",\"WindSpeed\":\"-\",\"WindDirec\":\"-\",\"PublishTime\":\"2020/12/18 18:00:00\",\"PM2.5_AVG\":\"19\",\"PM10_AVG\":\"29\",\"SO2_AVG\":\"3\",\"Longitude\":\"121.493806\",\"Latitude\":\"25.072611\",\"SiteId\":\"67\"},\n" +
"{\"SiteName\":\"三義\",\"County\":\"苗栗縣\",\"AQI\":\"26\",\"Pollutant\":\"\",\"Status\":\"良好\",\"SO2\":\"1.9\",\"CO\":\"0.25\",\"CO_8hr\":\"0.2\",\"O3\":\"21.3\",\"O3_8hr\":\"26\",\"PM10\":\"11\",\"PM2.5\":\"3\",\"NO2\":\"11.6\",\"NOx\":\"12.7\",\"NO\":\"1.2\",\"WindSpeed\":\"6\",\"WindDirec\":\"20\",\"PublishTime\":\"2020/12/18 18:00:00\",\"PM2.5_AVG\":\"8\",\"PM10_AVG\":\"12\",\"SO2_AVG\":\"2\",\"Longitude\":\"120.758833\",\"Latitude\":\"24.382942\",\"SiteId\":\"27\"}]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length();i++){
JSONObject jObj = jsonArray.getJSONObject(i);
String siteName = jObj.getString("SiteName");
String county = jObj.getString("County");
String aqi = jObj.getString("AQI");
//把JSON資料轉換為物件
Log.d("Howard","siteName:"+siteName+
" county:"+county+" aqi:"+aqi);
}
} catch (JSONException e) {
Log.e("Howard","JSONException:"+e);
}
}
}
然後也是從log看~
轉成VIEW看-
先新增PACKAGE-bean
裡面再新增CLASS-PmObj
對照前面的log的小寫填入:
Log.d("Howard","siteName:"+siteName+
" county:"+county+" aqi:"+aqi);
package com.huang.jsonagain1.bean;
public class PmObj {
private String siteName;
private String county;
private String aqi;
}
然後按右鍵 再加上generate
設定getter+setter
一個為getter用來取得private varible,另一個則為setter用來設定private varible
https://dotblogs.com.tw/law1009/2012/01/06/64665
然後按SHIFT+3個都選
package com.huang.jsonagain1.bean;
public class PmObj {
private String siteName;
private String county;
private String aqi;
public String getSiteName() {
return siteName;
}
public void setSiteName(String siteName) {
this.siteName = siteName;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
public String getAqi() {
return aqi;
}
public void setAqi(String aqi) {
this.aqi = aqi;
}
}
然後按右鍵 再加上generate-設定Constructor
然後也是按shift+3個全選
package com.huang.jsonagain1.bean;
public class PmObj {
private String siteName;
private String county;
private String aqi;
public PmObj(String siteName, String county, String aqi) {
this.siteName = siteName;
this.county = county;
this.aqi = aqi;
}
public String getSiteName() {
return siteName;
}
public void setSiteName(String siteName) {
this.siteName = siteName;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
public String getAqi() {
return aqi;
}
public void setAqi(String aqi) {
this.aqi = aqi;
}
}
然後再到最下面去toString按右鍵 再加上generate-去toString
去toString
然後也是全選
出來長這樣
package com.huang.jsonagain1.bean;
public class PmObj {
private String siteName;
private String county;
private String aqi;
public PmObj(String siteName, String county, String aqi) {
this.siteName = siteName;
this.county = county;
this.aqi = aqi;
}
public String getSiteName() {
return siteName;
}
public void setSiteName(String siteName) {
this.siteName = siteName;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
public String getAqi() {
return aqi;
}
public void setAqi(String aqi) {
this.aqi = aqi;
}
@Override
public String toString() {
return "PmObj{" +
"siteName='" + siteName + '\'' +
", county='" + county + '\'' +
", aqi='" + aqi + '\'' +
'}';
}
}
再回到MainActivity.java檔裡面加入 ArrayList
private ArrayList pmList = new ArrayList();
然後把另外一個檔案加入:
把JSON資料轉換為物件-自己手KEY
然後再貼入內容siteName,county,aqi:
自己手KEY--pmList.add(pmObj);
然後跑看看
這篇的下一篇:https://ithelp.ithome.com.tw/articles/10283969